﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SmoLiteApi
{
    /// <summary/>
    public class ServerConnection
    {
        private SqlConnection _sqlConnection;
        private string _serverInstance;

        /// <summary/>
        internal ServerConnection(SqlConnection sqlConnection)
        {
            this._sqlConnection = sqlConnection;
        }

        /// <summary/>
        public string ServerInstance
        {
            get { return this._serverInstance; }
            set { this._serverInstance = value; }
        }

        /// <summary/>
        public void Connect()
        {
            SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder();

            // set attributes
            sqlConnectionStringBuilder.IntegratedSecurity = true;
            sqlConnectionStringBuilder.Pooling = false;
            sqlConnectionStringBuilder.DataSource = this._serverInstance;

            // the connection string is needed before the connection can be opened
            this._sqlConnection.ConnectionString = sqlConnectionStringBuilder.ToString();

            // open the connection
            this._sqlConnection.Open();
        }

        /// <summary/>
        public void Disconnect()
        {
            this._sqlConnection.Close();
        }
    }
}
